#!/bin/bash

APP_NAME="dream_salted_fish"
APP_JAR="/usr/local/dreamsf/dsf.jar"
PID_FILE="/usr/local/dreamsf/dsf.pid"

start() {
    echo "Starting $APP_NAME..."

    if [ -f $PID_FILE ]; then
        if kill -9 $(cat $PID_FILE); then
            echo "$APP_NAME is already running."
        else
            rm -f $PID_FILE
        fi
    fi

    nohup java -jar $APP_JAR --spring.profiles.active=pro > /dev/null 2>&1 & echo $! > $PID_FILE

    echo "$APP_NAME started."
}

stop() {
    echo "Stopping $APP_NAME..."

    if [ ! -f $PID_FILE ]; then
        echo "$APP_NAME is not running."
    fi

    kill -9 $(cat $PID_FILE)
    rm -f $PID_FILE

    echo "$APP_NAME stopped."
}

status() {
    if [ -f $PID_FILE ]; then
        if kill -0 $(cat $PID_FILE); then
            echo "$APP_NAME is running."
        else
            echo "$APP_NAME is not running."
        fi
    else
        echo "$APP_NAME is not running."
    fi
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 1
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
esac